<?php

use console\db\Migration;
use yii\helpers\ArrayHelper;
use yii\helpers\Console;
use yii\db\Query;

/**
 * 初始化基本时段数据
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class m200110_001111_d_company_test extends Migration {

    public $tableName = '{{%company}}';

    /**
     * @inheritdoc
     */
    public function safeUp() {
        $this->compact = true;
        $string = $this->getData('company.dev.txt');
        $lines = explode("\n", $string);

        $data = [];
        foreach ($lines as $line) {
            if (!$line) {
                continue;
            }
            list($index, $province, $city, $dist, $substation, $voltage, $uuid) = explode("\t", $line);
            $data[] = [
                'index' => $index,
                'province' => $province,
                'city' => $city,
                'dist' => $dist,
                'substation' => $substation,
                'voltage' => $voltage,
                'uuid' => $uuid,
            ];
        }

        $companys = ArrayHelper::index($data, null, [
            function ($element) {
                return $element['province'];
            },
            'city',
            'dist'
        ]);

        foreach ($companys as $name => $item) {
            if (!is_array($item)) {
                continue;
            }
            $this->insert($this->tableName, [
                'name' => $name,
                'status' => 10,
                'created_at' => time(),
                'updated_at' => time(),
            ]);
            if (empty($item)) {
                continue;
            }
            $parentId = $this->db->getLastInsertID();
            $this->batchCreate($item, $parentId);
        }
    }

    /**
     * @inheritdoc
     */
    protected function batchCreate($data, $parentId = 0) {
        $count = count($data);
        Console::startProgress($i = 0, $count);
        foreach ($data as $name => $item) {
            $i++;
            Console::updateProgress($i, $count);
            if (!is_numeric($name)) {
                $this->insert($this->tableName, [
                    'parent_id' => $parentId,
                    'name' => $name,
                    'status' => 10,
                    'created_at' => time(),
                    'updated_at' => time(),
                ]);
                if (!empty($item)) {
                    $this->batchCreate($item, $this->db->getLastInsertID());
                }
            } else {
                $voltageName = ArrayHelper::getValue($item, 'voltage');
                $substationName = ArrayHelper::getValue($item, 'substation');
                $uuid = ArrayHelper::getValue($item, 'uuid');
                $voltage = (new Query())->from('{{%voltage}}')->andWhere([
                    'name' => $voltageName
                ])->one();
                if (!$voltage) {
                    $this->insert('{{%voltage}}', [
                        'name' => $voltageName,
                        'status' => 10,
                        'created_at' => time(),
                        'updated_at' => time(),
                    ]);
                    $voltageId = $this->db->getLastInsertID();
                } else {
                    $voltageId = ArrayHelper::getValue($voltage, 'id');
                }
                $substation = (new Query())->from('{{%substation}}')->andWhere([
                    'name' => $substationName,
                    'company_id' => $parentId,
                ])->one();
                if (!$substation) {
                    $this->insert('{{%substation}}', [
                        'company_id' => $parentId,
                        'name' => $substationName,
                        'voltage' => $voltageName,
                        'voltage_id' => $voltageId,
                        'uuid' => $uuid,
                        'status' => 10,
                        'created_at' => time(),
                        'updated_at' => time(),
                    ]);
                } else {
                    $this->update('{{%substation}}', [
                        'company_id' => $parentId,
                        'name' => $substationName,
                        'voltage' => $voltageName,
                        'voltage_id' => $voltageId,
                        'uuid' => $uuid,
                        'status' => 10,
                        'created_at' => time(),
                        'updated_at' => time(),
                    ], [
                        'id' => $substation['id']
                    ]);
                }
            }
        }
        Console::endProgress("update done." . PHP_EOL);
    }

}
